<!doctype html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>Leaflet debug page - Event Perf 2</title>
		<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0" />
		<link rel="stylesheet" href="../../dist/leaflet.css" />
		<link rel="stylesheet" href="../css/screen.css" />
		<script type="importmap">
			{
				"imports": {
					"leaflet": "../../dist/leaflet-src.esm.js"
				}
			}
		</script>
	</head>
	<body>
		<div id="map"></div>
		<script type="module">
			import {Map, TileLayer, FeatureGroup, LatLng, Marker} from 'leaflet';

			/*
			 * Can be used to profile performance of event system.
			 * Start the test, start CPU profiler. Slowly move the
			 * mouse around over the map for 20 seconds or so, stop
			 * profiler and examine times for _on, _off and fire.
			 */

			const map = new Map('map').setView([50.5, 30.51], 15);

			new TileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {maxZoom: 18}).addTo(map);

			const markers = new FeatureGroup().addTo(map);
			const markerArr = [];

			function getRandomLatLng(llbounds) {
				const s = llbounds.getSouth();
				const n = llbounds.getNorth();
				const w = llbounds.getWest();
				const e = llbounds.getEast();

				return new LatLng(
					s + Math.random() * (n - s),
					w + Math.random() * (e - w)
				);
			}

			function shuffleMarkers() {
				let marker;
				if (markerArr.length > 2000) {
					const i = Math.floor(Math.random() * markerArr.length);
					marker = markerArr.splice(i, 1)[0];
					markers.removeLayer(marker);
				} else {
					marker = new Marker(getRandomLatLng(map.getBounds()))
						.on('mouseover', () => {
							marker.setZIndexOffset(10000);
						})
						.on('mouseout', () => {
							marker.setZIndexOffset(0);
						})
						.addTo(markers);
					markerArr.push(marker);
				}
			}

			while (markerArr.length < 2000) {
				shuffleMarkers();
			}

			setInterval(shuffleMarkers, 20);
		</script>
	</body>
</html>
